/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.client.core.modal.grr;

import com.google.common.collect.Lists;
import com.intelligent.ispc.client.ex.dynamictable.DyAbstractTableModel;
import com.intelligent.ispc.client.utils.SystemConfigUtils;
import com.intelligent.ispc.client.utils.UIConstant;
import com.intelligent.ispc.common.utils.StringUtils;
import com.intelligent.ispc.core.dto.GrrSummaryDto;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * Created by cherry on 2016/8/24.
 */
public class SummaryTableModel extends DyAbstractTableModel<GrrSummaryDto> {

    private double[] rules;
    private String[] columns;
    private List<GrrSummaryDto> data;
    private int digGrrNum = UIConstant.DEFAULT_GRR_DIGNUM;
    private int digPercent = UIConstant.DEFAULT_DIGNUM_PERCENT;

    @Override
    public int getRowCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        if (column == 1 || column == 2) {
            return true;
        }
        return false;
    }

    @Override
    public void setValueAt(Object newValue, int row, int col) {
        if (data == null || data.size() <= row) {
            return;
        }

        GrrSummaryDto grrSummaryDto = data.get(row);
        switch (col) {
            case 1:
                if (checkValueValid(newValue, row, col)) {
                    grrSummaryDto.setLsl(newValue.toString());
                } else {
                    grrSummaryDto.setLsl("invalid data");
                }
                break;
            case 2:
                if (checkValueValid(newValue, row, col)) {
                    grrSummaryDto.setUsl(newValue.toString());
                } else {
                    grrSummaryDto.setUsl("invalid data");
                }
                break;
            default:
                break;

        }
    }

    @Override
    public int getColumnCount() {
        return columns == null ? 0 : columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return (columns == null) || (columns.length <= column) ? "" : columns[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

        if (data == null || data.size() < rowIndex) {
            return "-";
        }

        GrrSummaryDto grrSummaryDto = data.get(rowIndex);
        String itemName = grrSummaryDto.getItemName();
        String lsl = grrSummaryDto.getLsl();
        String usl = grrSummaryDto.getUsl();
        String tolerance = grrSummaryDto.getTolerance();
        String partVariation = grrSummaryDto.getPartVariation();
        String totalProcessVariation = grrSummaryDto.getTotalProcessVariation();
        String grr = grrSummaryDto.getGrr();
        String result = grrSummaryDto.getResult();
        String[] grrRules = UIConstant.ANALYSIS_GRR_RULES;
        switch (columnIndex) {
            case 0:
                if (StringUtils.isBlank(itemName)) {
                    return "-";
                } else {
                    return itemName;
                }
            case 1:
                if (StringUtils.isNumeric(lsl)) {
                    return lsl;
                } else {
                    return "-";
                }
            case 2:
                if (StringUtils.isNumeric(usl)) {
                    return usl;
                } else {
                    return "-";
                }
            case 3:
                if (StringUtils.isNumeric(tolerance)) {
                    return tolerance;
                } else {
                    return "-";
                }
            case 4:
                if (StringUtils.isNumeric(partVariation)) {
                    return StringUtils.formatDouble(Double.valueOf(partVariation), digPercent) + "%";
                } else {
                    return "-";
                }
            case 5:
                if (StringUtils.isNumeric(totalProcessVariation)) {
                    return StringUtils.formatDouble(Double.valueOf(totalProcessVariation), digPercent) + "%";
                } else {
                    return "-";
                }
            case 6:
                if (StringUtils.isNumeric(grr)) {
                    if (Double.valueOf(grr) < 0) {
                        return "-";
                    } else {
                        return "<html><u>" + StringUtils.formatDouble(Double.valueOf(grr), digPercent) + "%" + "</u></html>";
                    }
                } else {
                    return "-";
                }
            case 7:
                if (StringUtils.isBlank(result)) {
                    return "-";
                } else {

                    if ("EXCELLENT".equals(result)) {
                        return grrRules[0];
                    } else if ("GOOD".equals(result)) {
                        return grrRules[1];
                    } else if ("ACCEPTABLE".equals(result)) {
                        return grrRules[2];
                    } else if ("RECTIFICATION".equals(result)) {
                        return grrRules[3];
                    }

                    return result;
                }
            default:
                return "-";
        }
    }


    /**
     * SummaryTableModel constructor
     *
     * @param columns table columns
     */
    public SummaryTableModel(String[] columns) {
        if (columns != null) {
            this.columns = new String[columns.length];
            System.arraycopy(columns, 0, this.columns, 0, columns.length);
        }

        data = Lists.newArrayList();
    }

    /**
     * SummaryTableModel constructor
     */
    public SummaryTableModel() {
        data = Lists.newArrayList();
    }

    @Override
    public void setData(List<GrrSummaryDto> value) {
        if (value == null) {
            this.data = Lists.newArrayList();
        } else {
            this.data = value;
        }
        fireTableStructureChanged();
    }

    private boolean checkValueValid(Object newValue, int row, int col) {

        if (!StringUtils.isNumeric(newValue.toString())) {
            return false;
        }

        if (data != null && data.size() > row) {

            if (col == 1) {
                if (!StringUtils.isNumeric(data.get(row).getUsl())) {
                    return false;
                }

                double usl = Double.parseDouble(data.get(row).getUsl());
                double value = Double.parseDouble(newValue.toString());
                if (usl > value) {
                    return true;
                }

            } else if (col == 2) {
                if (!StringUtils.isNumeric(data.get(row).getLsl())) {
                    return false;
                }

                double lsl = Double.parseDouble(data.get(row).getLsl());
                double value = Double.parseDouble(newValue.toString());
                if (lsl < value) {
                    return true;
                }
            }
        }
        return false;
    }

    public List<GrrSummaryDto> getData() {
        return data;
    }

    /**
     * update this row data
     *
     * @param row           current row index is used
     * @param grrSummaryDto grrSummary object
     */
    public void setRowData(int row, GrrSummaryDto grrSummaryDto) {

        if (data == null || data.size() <= row) {
            return;
        }

        if (grrSummaryDto != null) {
            BeanUtils.copyProperties(grrSummaryDto, data.get(row));
        }

        fireTableDataChanged();
    }

    /**
     * @param rules grr config's rules
     */
    public void setRules(double[] rules) {
        if (rules == null) {
            this.rules = new double[0];
            return;
        }
        this.rules = new double[rules.length];
        System.arraycopy(rules, 0, this.rules, 0, rules.length);
    }

    /**
     * init digNum and digPercentNum value
     */
    public void initDigNum() {
        digGrrNum = SystemConfigUtils.getInstance().getGrrDigit();
        digPercent = (digGrrNum >= 2) ? (digGrrNum - 2) : 0;
    }

    /**
     * @param columns summary table's columns name
     */
    public void setColumns(String[] columns) {
        if (columns == null) {
            this.columns = columns;
            return;
        }
        this.columns = new String[columns.length];
        System.arraycopy(columns, 0, this.columns, 0, columns.length);
    }
}
